package com.baizhi.hospital.controller;

import com.baizhi.hospital.util.ValidationCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
public class ValidationCodeController {

	@RequestMapping("/createCode")
	private void createCode(HttpSession session, HttpServletResponse response) throws IOException {
//		方法返回的两个数据，在一个object数组中，第一个是文字内容，第二个是图片对象
		Object[] image = ValidationCodeUtil.createImage();
		System.out.println(image[0]);

//		将验证码存入session
		session.setAttribute("code",image[0]);

//		将图片输出给浏览器
		BufferedImage img = (BufferedImage)image[1];
		response.setContentType("image/png");
		ServletOutputStream outputStream = response.getOutputStream();
		ImageIO.write(img,"png",outputStream);

	}
}
